//
//  YBEarnDetailInviteEarnTableViewCell.m
//  Nectar
//
//  Created by famer on 2023/11/11.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBEarnDetailInviteEarnTableViewCell.h"

@implementation YBEarnDetailInviteEarnTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        self.contentView.backgroundColor = kThemeBackground;
        
        _bgView = [[UIView alloc]initWithFrame:CGRectZero];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 10;
        _bgView.backgroundColor = kWhiteColor;
        [self.contentView addSubview:_bgView];
        
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.font = [UIFont systemFontOfSize:12];
        _dateLabel.text = @"*";
        _dateLabel.textColor = [UIColor hexColor:@"84828C"];
        _dateLabel.textAlignment = NSTextAlignmentRight;
        _dateLabel.numberOfLines = 0;
        _dateLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_dateLabel];
        
        _earnAmountLabel = [[UILabel alloc] init];
        _earnAmountLabel.text = @"Earn Amount";
        _earnAmountLabel.font = [UIFont systemFontOfSize:12];
        _earnAmountLabel.textColor = [UIColor hexColor:@"84828C"];
        _earnAmountLabel.textAlignment = NSTextAlignmentLeft;
        _earnAmountLabel.numberOfLines = 0;
        _earnAmountLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_earnAmountLabel];
        
        _earnAmountValueLabel = [[UILabel alloc] init];
        _earnAmountValueLabel.text = @"*";
        _earnAmountValueLabel.font = [UIFont boldSystemFontOfSize:14];
        _earnAmountValueLabel.textColor = kThemeBlackColor;
        _earnAmountValueLabel.textAlignment = NSTextAlignmentRight;
        _earnAmountValueLabel.numberOfLines = 0;
        _earnAmountValueLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_earnAmountValueLabel];
        
        _inviteeAccountNameLabel = [[UILabel alloc] init];
        _inviteeAccountNameLabel.text = @"Invitee account name";
        _inviteeAccountNameLabel.font = [UIFont systemFontOfSize:12];
        _inviteeAccountNameLabel.textColor = [UIColor hexColor:@"84828C"];
        _inviteeAccountNameLabel.textAlignment = NSTextAlignmentLeft;
        _inviteeAccountNameLabel.numberOfLines = 0;
        _inviteeAccountNameLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_inviteeAccountNameLabel];
        
        _inviteeAccountNameValueLabel = [[UILabel alloc] init];
        _inviteeAccountNameValueLabel.text = @"*";
        _inviteeAccountNameValueLabel.font = [UIFont boldSystemFontOfSize:12];
        _inviteeAccountNameValueLabel.textColor = [UIColor hexColor:@"84828C"];
        _inviteeAccountNameValueLabel.textAlignment = NSTextAlignmentRight;
        _inviteeAccountNameValueLabel.numberOfLines = 0;
        _inviteeAccountNameValueLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_inviteeAccountNameValueLabel];
        
        _earnRatioLabel = [[UILabel alloc] init];
        _earnRatioLabel.text = @"Earn ratio";
        _earnRatioLabel.font = [UIFont systemFontOfSize:12];
        _earnRatioLabel.textColor = [UIColor hexColor:@"84828C"];
        _earnRatioLabel.textAlignment = NSTextAlignmentLeft;
        _earnRatioLabel.numberOfLines = 0;
        _earnRatioLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_earnRatioLabel];
        
        _earnRatioValueLabel = [[UILabel alloc] init];
        _earnRatioValueLabel.text = @"*";
        _earnRatioValueLabel.font = [UIFont boldSystemFontOfSize:12];
        _earnRatioValueLabel.textColor = [UIColor hexColor:@"84828C"];
        _earnRatioValueLabel.textAlignment = NSTextAlignmentRight;
        _earnRatioValueLabel.numberOfLines = 0;
        _earnRatioValueLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [_bgView addSubview:_earnRatioValueLabel];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.size.width;
    CGFloat height = self.contentView.size.height;
    CGFloat leftMargin = 12;
    CGFloat bgWidth = width-leftMargin*2;
    CGFloat rowWidth = bgWidth-leftMargin*2;
    
    
    self.bgView.frame = CGRectMake(leftMargin, 12, bgWidth, 0);
    
    CGSize dateLabelSize = [self.dateLabel textBoundingSizeWithMaxSize:CGSizeMake(rowWidth, 500)];
    self.dateLabel.frame = CGRectMake(leftMargin,
                                      leftMargin,
                                      rowWidth,
                                      dateLabelSize.height);
    
    CGSize earnAmountLabelSize = [self.earnAmountLabel textBoundingSizeWithMaxSize:CGSizeMake(bgWidth, 500)];
    self.earnAmountLabel.frame = CGRectMake(leftMargin,
                                            self.dateLabel.bottom+10,
                                            rowWidth,
                                            earnAmountLabelSize.height);
    
    CGSize earnAmountValueLabelSize = [self.earnAmountValueLabel textBoundingSizeWithMaxSize:CGSizeMake(rowWidth, 500)];
    self.earnAmountValueLabel.frame = CGRectMake(leftMargin,
                                                 self.dateLabel.bottom+10,
                                                 rowWidth,
                                                 earnAmountValueLabelSize.height);
    
    self.earnAmountLabel.centerY = self.earnAmountValueLabel.centerY;
    
    ///
    CGSize transferInAmountLabelSize = [self.inviteeAccountNameLabel textBoundingSizeWithMaxSize:CGSizeMake(bgWidth, 500)];
    self.inviteeAccountNameLabel.frame = CGRectMake(leftMargin,
                                                    self.earnAmountValueLabel.bottom+10,
                                                    rowWidth,
                                                    transferInAmountLabelSize.height);
    
    CGSize transferInAmountValueLabelSize = [self.inviteeAccountNameValueLabel textBoundingSizeWithMaxSize:CGSizeMake(rowWidth, 500)];
    self.inviteeAccountNameValueLabel.frame = CGRectMake(leftMargin,
                                                         self.earnAmountValueLabel.bottom+10,
                                                         rowWidth,
                                                         transferInAmountValueLabelSize.height);
    
    self.inviteeAccountNameLabel.centerY = self.inviteeAccountNameValueLabel.centerY;
    
    ///
    CGSize earnRatioLabelSize = [self.earnRatioLabel textBoundingSizeWithMaxSize:CGSizeMake(bgWidth, 500)];
    self.earnRatioLabel.frame = CGRectMake(leftMargin,
                                           self.inviteeAccountNameValueLabel.bottom+10,
                                           rowWidth,
                                           earnRatioLabelSize.height);
    
    CGSize earnRatioValueLabelSize = [self.earnRatioValueLabel textBoundingSizeWithMaxSize:CGSizeMake(rowWidth, 500)];
    self.earnRatioValueLabel.frame = CGRectMake(leftMargin,
                                                self.inviteeAccountNameValueLabel.bottom+10,
                                                rowWidth,
                                                earnRatioValueLabelSize.height);
    
    self.earnRatioLabel.centerY = self.earnRatioValueLabel.centerY;
    
    self.bgView.height= self.earnRatioValueLabel.bottom+12;
}

- (void)setData:(RevDataGetInviteIncomeList *)item{
    
    if(item!=nil){
        self.dateLabel.text = item.time;
        self.earnAmountValueLabel.text = [NSString stringWithFormat:@"%@ USDT", item.income];
        self.inviteeAccountNameValueLabel.text = [NSString stringWithFormat:@"%@", item.email];
        self.earnRatioValueLabel.text = [NSString stringWithFormat:@"%@", [item.rate toFinancePercentString]];
        
        [self setNeedsLayout];
        [self layoutIfNeeded];
    }
}

@end
